﻿using System;
using System.Collections.Generic;
using System.Text;

namespace CPPEI.Coolzon.ViewState
{
    public static class ScenePathUtil
    {
        /// <summary>
        /// 
        /// ScenePath
        /// ex:
        ///     open:>s1?args1/pop:>s2?args2
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string ToString(ScenePath path)
        {
            if (path == null)
            {
                return string.Empty;
            }

            StringBuilder builder = new StringBuilder();

            ScenePath curPath = path;
            while (curPath != null)
            {
                builder.AppendFormat("/{0}:>{1}?{2}", curPath.OpenMode, curPath.SceneName, curPath.Arguments);
                curPath = curPath.Parent;
            }

            return builder.ToString();
        }

        public static ScenePath FromString(string pathString)
        {
            ScenePath parent = null;
            string[] segments = pathString.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
            for (int index = segments.Length - 1; index >= 0; index--)
            {
                string[] pathArr = segments[index].Split(new char[] { '?' }, StringSplitOptions.RemoveEmptyEntries);
                if (pathArr.Length == 2)
                {
                    string[] s1Arr = pathArr[0].Split(new string[] { ":>" }, StringSplitOptions.RemoveEmptyEntries);

                    ScenePath path = new ScenePath(s1Arr[0], s1Arr[1], pathArr[1], parent);
                    parent = path;
                }
                else
                {
                    string[] s1Arr = pathArr[0].Split(new string[] { ":>" }, StringSplitOptions.RemoveEmptyEntries);
                    ScenePath path = new ScenePath(s1Arr[0], s1Arr[1], parent);
                    parent = path;
                }
            }

            return parent;
        }
    }
}
